package com.dstats;

import java.io.IOException;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.entities.Hero;
import com.entities.Item;
import com.entities.MatchDetailed;
import com.entities.PlayerDetailed;
import com.entities.SteamUser;
import com.fetch.ImageLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MatchDetailsActivity extends Activity {

	
	
	private static final String _LG = "_lg";
	private static final String _PNG = ".png";
	private static MatchDetailed match;
	private TextView whoWon;
	private HashMap<String, SteamUser> steamUserMap = SteamUser.getSteamUserOfJsonObj(match.getPlayers());
	private ImageLoader imageLoaderItems;
	private String accId;
	private ImageLoader imageLoaderHeroes;
	public static void setMatch(MatchDetailed match) {
		MatchDetailsActivity.match = match;
	}
	
	
	
	static class ViewHolder{
		private ImageView heroImg;
		private TextView lvl;
		private TextView kill;
		private TextView death;
		private TextView assists;
		private TextView lh;
		private TextView dn;
		private TextView hd;
		private TextView gpm;
		private TextView xpm;
		private TextView gold_spent;
		private TextView hero_damage;
		private TextView tower_damage;
		private TextView hero_healing;
		private TextView gold;
		
		ViewHolder(Context context){
			heroImg = new ImageView(context);
			lvl = new TextView(context);
			lvl.setTextColor(Color.BLACK);
			kill = new TextView(context);
			kill.setTextColor(Color.BLACK);
			death = new TextView(context);
			death.setTextColor(Color.BLACK);
			assists = new TextView(context);
			assists.setTextColor(Color.BLACK);
			lh = new TextView(context);
			lh.setTextColor(Color.BLACK);
			dn = new TextView(context);
			dn.setTextColor(Color.BLACK);
			hd = new TextView(context);
			hd.setTextColor(Color.BLACK);
			gpm = new TextView(context);
			gpm.setTextColor(Color.BLACK);
			xpm = new TextView(context);
			xpm.setTextColor(Color.BLACK);
			gold_spent = new TextView(context);
			gold_spent.setTextColor(Color.BLACK);
			gold = new TextView(context);
			gold.setTextColor(Color.BLACK);
			hero_damage = new TextView(context);
			hero_damage.setTextColor(Color.BLACK);
			tower_damage = new TextView(context);
			tower_damage.setTextColor(Color.BLACK);
			hero_healing = new TextView(context);
			hero_healing.setTextColor(Color.BLACK);
		}
	}
	

	private void fillTableRowItems(TableRow row, PlayerDetailed player, SparseArray<String> sparseArray){

		LayoutParams paramasRowImg= new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
		
		ImageView item0 = new ImageView(getApplicationContext());
		ImageView item1 = new ImageView(getApplicationContext());
		ImageView item2 = new ImageView(getApplicationContext());
		ImageView item3 = new ImageView(getApplicationContext());
		ImageView item4 = new ImageView(getApplicationContext());
		ImageView item5 = new ImageView(getApplicationContext());
		
		paramasRowImg.setMargins(0, 5, 0, 5);
		
		int rescale_h = 24;
		int rescale_w = 33;
		
		String imgString0 = sparseArray.get(player.getItem_0()) +_LG;
		String drawable = "drawable";
		int imgIdentifier0 = getApplicationContext().getResources().getIdentifier(imgString0, drawable, getApplicationContext().getPackageName());
		if(imgIdentifier0!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier0);
		item0.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			String urlItem0 = Item.baseImgUrl + imgString0 + _PNG;
			imageLoaderItems.DisplayImage(urlItem0, item0, rescale_h, rescale_w);
		}
		
		String imgString1 = sparseArray.get(player.getItem_1()) +_LG;
		int imgIdentifier1 = getApplicationContext().getResources().getIdentifier(imgString1, drawable, getApplicationContext().getPackageName());
		if(imgIdentifier1!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier1);
		item1.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			String urlItem1 = Item.baseImgUrl + imgString1 + _PNG;
			imageLoaderItems.DisplayImage(urlItem1, item1, rescale_h, rescale_w);
		}
		
		
		String imgString2 = sparseArray.get(player.getItem_2()) +_LG;
		int imgIdentifier2 = getApplicationContext().getResources().getIdentifier(imgString2, drawable, getApplicationContext().getPackageName());
		if(imgIdentifier2!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier2);
		item2.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			String urlItem2 = Item.baseImgUrl + imgString2 + _PNG;
			imageLoaderItems.DisplayImage(urlItem2, item2, rescale_h, rescale_w);
		}
		
		String imgString3 = sparseArray.get(player.getItem_3()) +_LG;
		int imgIdentifier3 = getApplicationContext().getResources().getIdentifier(imgString3, drawable, getApplicationContext().getPackageName());
		if(imgIdentifier3!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier3);
		item3.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			String urlItem3 = Item.baseImgUrl + imgString3 + _PNG;
			imageLoaderItems.DisplayImage(urlItem3, item3, rescale_h, rescale_w);
		}
		
		String imgString4 = sparseArray.get(player.getItem_4()) +_LG;
		int imgIdentifier4 = getApplicationContext().getResources().getIdentifier(imgString4, drawable, getApplicationContext().getPackageName());
		if(imgIdentifier4!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier4);
		item4.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			String urlItem4 = Item.baseImgUrl + imgString4 + _PNG;
			imageLoaderItems.DisplayImage(urlItem4, item4, rescale_h, rescale_w);
		}
		
		String imgString5 = sparseArray.get(player.getItem_5()) +_LG;
		int imgIdentifier5 = getApplicationContext().getResources().getIdentifier(imgString5, drawable, getApplicationContext().getPackageName());
		if(imgIdentifier5!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier5);
		item5.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			String urlItem5 = Item.baseImgUrl + imgString5 + _PNG;
			imageLoaderItems.DisplayImage(urlItem5, item5, rescale_h, rescale_w);
		}
		
		
		TextView tv = new TextView(getApplicationContext());
		SteamUser steamUser = steamUserMap.get(String.valueOf(player.getAccount_id()));
		if(steamUser!=null){
		String personaname = steamUser.getPersonaname();
		if(personaname.length()>=9){
			CharSequence cs=  personaname.subSequence(0, 9);
			tv.setText(cs+"...");
		} else {
			tv.setText(personaname);

		}
			tv.setTextColor(Color.BLACK);
		} else {
			tv.setText("Anonymous");
		}
		tv.setGravity(Gravity.CENTER);
		row.addView(tv);
		row.addView(item0);
		row.addView(item1);
		row.addView(item2);
		row.addView(item3);
		row.addView(item4);
		row.addView(item5);
		
		if(accId!=null && String.valueOf(player.getAccount_id()).equals(accId)){
			row.setBackgroundColor(Color.rgb(255, 204, 204));
		}
	}
	
	private String stringValueInK(int value){
		
		String valueString = String.valueOf(value);
		if(value/1000 >= 1 ){
			if(value/100 - value/1000*10 !=0){
				valueString = String.valueOf(value/1000) + "." + String.valueOf(value/100 - value/1000*10 ) + "k";	
			} else {
				valueString = String.valueOf(value/1000) + "k";	
			}	
		}
		return valueString;
	}
	
	private void fillTableRow(TableRow row, final PlayerDetailed player){
		LayoutParams paramasRowText = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
		LayoutParams paramasRowImg= new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

		ViewHolder holder = new ViewHolder(getApplicationContext());
		
		paramasRowImg.setMargins(0, 5, 0, 5);
		row.addView(holder.heroImg, paramasRowImg);

		
		Hero hero = Hero.getHeroById(getApplicationContext().getString(R.string.language), player.getHero_id());
		
		int rescale_h = 45;
		int rescale_w = 83;
		int imgIdentifier = getApplicationContext().getResources().getIdentifier(hero.getImgResString(), "drawable", getApplicationContext().getPackageName());
		if(imgIdentifier!=0){
		Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), imgIdentifier);
		holder.heroImg.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
		} else {
			imageLoaderHeroes.DisplayImage(hero.getImgLgUrl(), holder.heroImg, rescale_h, rescale_w);
		}
		
		
		paramasRowText.setMargins(21, 10, 10, 0);
		
		
		holder.lvl.setText(String.valueOf(player.getLevel()));
		holder.kill.setText(String.valueOf(player.getKills()));
		holder.death.setText(String.valueOf(player.getDeaths()));
		holder.assists.setText(String.valueOf(player.getAssists()));
		holder.lh.setText(String.valueOf(player.getLast_hits()));
		holder.dn.setText(String.valueOf(player.getDenies()));
		holder.hd.setText(stringValueInK(player.getHero_damage()));
		holder.gpm.setText(String.valueOf(player.getGpm()));
		holder.xpm.setText(String.valueOf(player.getXpm()));
		holder.tower_damage.setText(stringValueInK(player.getTower_damage()));
		holder.hero_healing.setText(stringValueInK(player.getHero_healing()));		
		holder.gold.setText(stringValueInK(player.getGold()+player.getGold_spent()));
		
		
		row.addView(holder.lvl, paramasRowText);
		row.addView(holder.kill, paramasRowText);
		row.addView(holder.death, paramasRowText);
		row.addView(holder.assists, paramasRowText);
		row.addView(holder.lh, paramasRowText);
		row.addView(holder.dn, paramasRowText);
		row.addView(holder.hd, paramasRowText);
		row.addView(holder.gpm, paramasRowText);
		row.addView(holder.xpm, paramasRowText);
		row.addView(holder.gold, paramasRowText);
		row.addView(holder.tower_damage, paramasRowText);
		row.addView(holder.hero_healing, paramasRowText);
		if(accId!=null && String.valueOf(player.getAccount_id()).equals(accId)){
			row.setBackgroundColor(Color.rgb(255, 204, 204));
		}
		
		row.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(getApplicationContext(), MatchsActivity.class);
				intent.putExtra("accId", player.getAccount_id().toString());
				startActivity(intent);
				
			}
		});
	}
	
	private void fillTableTitle(TableRow row, String teamName){
		LayoutParams paramasRowText = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
		LayoutParams paramasRowTeamTitle= new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
		
		ViewHolder holder = new ViewHolder(getApplicationContext());
		TextView title = new TextView(getApplicationContext());
		
		title.setText(teamName);
		title.setTextColor(Color.BLACK);
		paramasRowTeamTitle.setMargins(0, 10, 0, 0);
		
		row.addView(title, paramasRowTeamTitle);
		
		paramasRowText.setMargins(21, 5, 10, 5);
		holder.lvl.setText("L");
		holder.kill.setText("K");
		holder.death.setText("D");
		holder.assists.setText("A");
		holder.lh.setText("LH");
		holder.dn.setText("DN");
		holder.hd.setText("HD");
		holder.gpm.setText("GPM");
		holder.xpm.setText("XMP");
		holder.gold_spent.setText("GS");
		holder.tower_damage.setText("TD");
		holder.hero_healing.setText("HH");
		holder.gold.setText("GOLD");

		row.addView(holder.lvl, paramasRowText);
		row.addView(holder.kill, paramasRowText);
		row.addView(holder.death, paramasRowText);
		row.addView(holder.assists, paramasRowText);
		row.addView(holder.lh, paramasRowText);
		row.addView(holder.dn, paramasRowText);
		row.addView(holder.hd, paramasRowText);
		row.addView(holder.gpm, paramasRowText);
		row.addView(holder.xpm, paramasRowText);
		row.addView(holder.gold, paramasRowText);
		row.addView(holder.tower_damage, paramasRowText);
		row.addView(holder.hero_healing, paramasRowText);

		
	}
	
	private SparseArray<String> getItems(){
		SparseArray<String> items = null;
		try {
			 items = Item.getAllItemsAsSparseArray(getAssets().open("items.json"));

		} catch (IOException e) {
		}
		return items;
	}
	
	private AdView mAdView;


    @Override
    protected void onPause() {
        mAdView.pause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAdView.resume();
    }

    @Override
    protected void onDestroy() {
        mAdView.destroy();
        super.onDestroy();
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		accId = getIntent().getStringExtra("curAcc");
		setContentView(R.layout.fragment_match_details);
		whoWon = (TextView) findViewById(R.id.textViewWhoWon);
        mAdView = (AdView) findViewById(R.id.adViewMatchDetails);
        mAdView.loadAd(new AdRequest.Builder().build());
		if(imageLoaderItems==null)
		imageLoaderItems = new ImageLoader(getApplicationContext());
		if(imageLoaderHeroes==null)
			imageLoaderHeroes = new ImageLoader(getApplicationContext());


			

		if(match.isRadiant_win()) {
			if(match.getRadiant_name().isEmpty()){
			whoWon.setText(getString(R.string.radiantVictory).toUpperCase());
			} else {
				whoWon.setText(match.getRadiant_name().toUpperCase() + " VICTORY");
			}
		}
		else {
			if(match.getDire_name().isEmpty()){
			whoWon.setText(getString(R.string.direVictory).toUpperCase());
			} else {
			whoWon.setText(match.getDire_name().toUpperCase() + " VICTORY");
			}
		}
		
		TableRow rowTitle = (TableRow) findViewById(R.id.tableRowTitle);
		
		TableRow rowSlot0 = (TableRow) findViewById(R.id.tableRowSlot0);
		TableRow rowSlot1 = (TableRow) findViewById(R.id.tableRowSlot1);
		TableRow rowSlot2 = (TableRow) findViewById(R.id.tableRowSlot2);
		TableRow rowSlot3 = (TableRow) findViewById(R.id.tableRowSlot3);
		TableRow rowSlot4 = (TableRow) findViewById(R.id.tableRowSlot4);
		TableRow rowSlot128 = (TableRow) findViewById(R.id.tableRowSlot128);
		TableRow rowSlot129 = (TableRow) findViewById(R.id.tableRowSlot129);
		TableRow rowSlot130 = (TableRow) findViewById(R.id.tableRowSlot130);
		TableRow rowSlot131 = (TableRow) findViewById(R.id.tableRowSlot131);
		TableRow rowSlot132 = (TableRow) findViewById(R.id.tableRowSlot132);
		TableRow rowTitleDire = (TableRow) findViewById(R.id.tableRowTitleDire);
	
		TableRow rowItemsOfSlot0 = (TableRow) findViewById(R.id.tableRowItemsOfSlot0);
		TableRow rowItemsOfSlot1 = (TableRow) findViewById(R.id.tableRowItemsOfSlot1);
		TableRow rowItemsOfSlot2 = (TableRow) findViewById(R.id.tableRowItemsOfSlot2);
		TableRow rowItemsOfSlot3 = (TableRow) findViewById(R.id.tableRowItemsOfSlot3);
		TableRow rowItemsOfSlot4 = (TableRow) findViewById(R.id.tableRowItemsOfSlot4);
		TableRow rowItemsOfSlot128 = (TableRow) findViewById(R.id.tableRowItemsOfSlot128);
		TableRow rowItemsOfSlot129 = (TableRow) findViewById(R.id.tableRowItemsOfSlot129);
		TableRow rowItemsOfSlot130 = (TableRow) findViewById(R.id.tableRowItemsOfSlot130);
		TableRow rowItemsOfSlot131 = (TableRow) findViewById(R.id.tableRowItemsOfSlot131);
		TableRow rowItemsOfSlot132 = (TableRow) findViewById(R.id.tableRowItemsOfSlot132);
			
		
		
		SparseArray<PlayerDetailed> players = match.getSparseArrayPlayers();
	
		steamUserMap = SteamUser.getSteamUserOfJsonObj(match.getPlayers());
		if(match.getRadiant_name().isEmpty()){
		fillTableTitle(rowTitle, getString(R.string.radiant).toUpperCase());
		} else {
			fillTableTitle(rowTitle, match.getRadiant_name().toUpperCase());	
		}
		fillTableRow(rowSlot0, players.get(0));
		
		fillTableRow(rowSlot1, players.get(1));
		rowSlot1.setBackgroundColor(Color.LTGRAY);
		
		fillTableRow(rowSlot2, players.get(2));
		
		fillTableRow(rowSlot3, players.get(3));
		rowSlot3.setBackgroundColor(Color.LTGRAY);
		
		fillTableRow(rowSlot4, players.get(4));
		
		if(match.getDire_name().isEmpty()){
		fillTableTitle(rowTitleDire, getString(R.string.dire).toUpperCase());
		} else {
			fillTableTitle(rowTitleDire, match.getDire_name().toUpperCase());	
		}
		
		rowSlot128.setBackgroundColor(Color.LTGRAY);
		fillTableRow(rowSlot128, players.get(128));
		
		fillTableRow(rowSlot129, players.get(129));
		
		rowSlot130.setBackgroundColor(Color.LTGRAY);
		fillTableRow(rowSlot130, players.get(130));
		
		fillTableRow(rowSlot131, players.get(131));
		
		rowSlot132.setBackgroundColor(Color.LTGRAY);
		fillTableRow(rowSlot132, players.get(132));
		
		
		rowItemsOfSlot1.setBackgroundColor(Color.LTGRAY);
		rowItemsOfSlot3.setBackgroundColor(Color.LTGRAY);
		rowItemsOfSlot128.setBackgroundColor(Color.LTGRAY);
		rowItemsOfSlot130.setBackgroundColor(Color.LTGRAY);
		rowItemsOfSlot132.setBackgroundColor(Color.LTGRAY);
		

		fillTableRowItems(rowItemsOfSlot0, players.get(0), getItems());
		fillTableRowItems(rowItemsOfSlot1, players.get(1), getItems());
		fillTableRowItems(rowItemsOfSlot2, players.get(2), getItems());
		fillTableRowItems(rowItemsOfSlot3, players.get(3), getItems());
		fillTableRowItems(rowItemsOfSlot4, players.get(4), getItems());
		
		fillTableRowItems(rowItemsOfSlot128, players.get(128), getItems());
		fillTableRowItems(rowItemsOfSlot129, players.get(129), getItems());
		fillTableRowItems(rowItemsOfSlot130, players.get(130), getItems());
		fillTableRowItems(rowItemsOfSlot131, players.get(131), getItems());
		fillTableRowItems(rowItemsOfSlot132, players.get(132), getItems());

	}





}

